package com.boge.core.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.boge.core.common.AjaxResult;
import com.boge.core.model.dao.mappers.UserMapper;
import com.boge.core.model.entity.User;
import com.boge.core.service.UserService;
import com.boge.util.myutils.ShortMessageTool;
import jdk.internal.dynalink.beans.StaticClass;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/register")
public class RegisterController {
	
	@Autowired
	UserMapper userMapper;

	@Autowired
	UserService userService;
	
	@RequestMapping
	public String register() {
		return "register";
	}
	
	@RequestMapping("/test")
	@ResponseBody
	public AjaxResult test(HttpServletRequest request, HttpServletResponse response){
		try {
			response.sendRedirect(request.getAttribute("basePath") + "home");
		} catch (IOException e) {
			e.printStackTrace();
		}
		return new AjaxResult();
	}
	
	@RequestMapping("/sendSms")
	@ResponseBody
	public AjaxResult sendSms(HttpServletRequest request){
		AjaxResult ar = new AjaxResult();
		Map<String, String> map = new HashMap<String, String>();
		String phone = request.getParameter("phone");
		System.err.println("phone:" + phone);
		SendSmsResponse sendSmsResponse = ShortMessageTool.sendSms(phone);
		if(sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals("OK")) {
			ar.setData(phone);
		} else {
			ar.setStatus(false);
		}
		return ar;
	}
	
	@RequestMapping("/queryLastVerificationCode")
	@ResponseBody
	public boolean queryVerificationCode(HttpServletRequest request){
		String phone = request.getParameter("phone");
		String verificationCodeInput = request.getParameter("verificationCode");
		System.out.println("phonephone:" + phone);
		System.out.println("verificationCodeInputverificationCodeInput:" + verificationCodeInput);
		String verificationCode = ShortMessageTool.queryLastVerificationCode(phone);
		System.out.println("verificationCode:" + verificationCode);
		if (verificationCode != null && verificationCodeInput.equals(verificationCode)) {
			return true;
		}
		return false;
	}
	
	@RequestMapping("/verification")
	public void verification(HttpServletRequest request) {
		String phone = request.getParameter("phone");
		String vericationCode = request.getParameter("vericationCode");
		String userName = request.getParameter("userName");
		String password = request.getParameter("password");
		String repeatPassword = request.getParameter("repeatPassword");
		String agree = request.getParameter("agree");
		
	}
	
	@RequestMapping("/addUser")
	@ResponseBody
	public AjaxResult addUser(HttpServletRequest request, HttpServletResponse response) {
		System.out.println("phone:");
		String phone = request.getParameter("phone");
		String userName = request.getParameter("userName");
		String password = request.getParameter("password");
		System.out.println("phone:" + phone);
		System.out.println("userName:" + userName);
		
		User user = new User();
		user.setUserName(userName);
		user.setUserPassword(password);
		user.setUserType(0);
		user.setUserPhone(phone);
		int id = userService.addUser(user);
		if (id > 0) {
			return new AjaxResult();
		}
		return new AjaxResult(false);
		
	}
	
	@RequestMapping("/validateUserName")
	@ResponseBody
	public AjaxResult validateUserName(HttpServletRequest request) {
		AjaxResult ajaxResult = new AjaxResult();
		String userName = request.getParameter("userName");
		System.out.println("userName:" + userName);
		User user = userService.findUserByUserName(userName);
		if (user != null) {
			ajaxResult.setStatus(true);
		} else {
			ajaxResult.setStatus(false);
		}
		return ajaxResult;
		
	}
	
}
